package com.bosssoft.goods.service.impl;

import com.bosssoft.base.entity.PaginationData;
import com.bosssoft.goods.dao.GoodsBrandDao;
import com.bosssoft.goods.domain.bean.CatBrandBind;
import com.bosssoft.goods.domain.bean.GoodsBrand;
import com.bosssoft.goods.domain.vo.EditBrandVO;

import com.bosssoft.goods.service.GoodsBrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品品牌操作服务接口实现
 * @author Fang_Sir
 * @date 2021-04-13
 */
@Service
public class GoodsBrandServiceImpl implements GoodsBrandService {
    @Autowired
    private GoodsBrandDao goodsBrandDao;

    @Override
    public PaginationData<GoodsBrand> getBrandList(int page, int size) {
        PageHelper.startPage(page,size);
        List<GoodsBrand> result = goodsBrandDao.getBrandList();
        PageInfo<GoodsBrand> info = new PageInfo<>(result);
        return new PaginationData<>(page,info.getTotal(),info.getList());
    }

    @Override
    public GoodsBrand getBrandInfo(String name) {
        return goodsBrandDao.getBrandByName(name);
    }

    @Override
    public boolean addBrand(String name) {
        if(goodsBrandDao.isBrandExist(name) <= 0){
            return goodsBrandDao.addBrand(name, 10000000) > 0;
        }
        return false;
    }

    @Override
    public boolean isExistBrand(String name) {
        return goodsBrandDao.isBrandExist(name) > 0;
    }

    @Override
    public boolean editBrand(EditBrandVO info) throws Exception{
        if(goodsBrandDao.isBrandExist(info.getName()) > 0) {
            if(goodsBrandDao.getBrandByName(info.getName()).getId() != info.getId()){
                throw new Exception("该品牌已存在！");
            }
        }
        return goodsBrandDao.editBrand(info) > 0;
    }

    @Override
    public boolean delBrand(int brandId) {
        return goodsBrandDao.delBrand(brandId) > 0;
    }

    @Override
    public List<CatBrandBind> getCatBrandList(int catId) {
        return goodsBrandDao.getCatBrandList(catId);
    }

    @Override
    public boolean bindCatBrand(int catId, int brandId) throws Exception {
        if(!isCatBrandBind(catId, brandId)) {
            return goodsBrandDao.bindCatBrand(catId, brandId) > 0;
        }
        return false;
    }

    @Override
    public boolean isCatBrandBind(int catId, int brandId) {
        return goodsBrandDao.isCatBrandBind(catId, brandId) > 0;
    }

    @Override
    public boolean removeCategoryBrand(int catId, int brandId) {
        return goodsBrandDao.removeCatBrandBind(catId, brandId) > 0;
    }
}
